<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="150px">
      <el-form-item label="应用名称" prop="name" required>
        <el-input v-model="form.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="图标" prop="icon" required>
        <el-input v-model="form.icon" placeholder="请输入图标" />
      </el-form-item>

      <el-form-item label="插件标签" prop="plugin_tag" required>
        <el-input v-model="form.plugin_tag" placeholder="请输入标签" />
      </el-form-item>
      <el-form-item label="API最低可用版本" prop="min_version" required>
        <el-input v-model="form.min_version" type="number" placeholder="API接口版本code，如：141" />
      </el-form-item>

      <el-form-item label="状态" prop="status" required>
        <el-radio-group v-model="form.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="插件路径"  >
        <el-input v-model="form.path" placeholder="请输入插件路径" />
      </el-form-item>
      <el-form-item label="简单描述" prop="remark" required>
        <el-input type="textarea" rows="3" v-model="form.remark" placeholder="请输入简单描述" />
      </el-form-item>

      <el-form-item label="详情说明" prop="content" required>
         <ueditor ref="ueditor" :config="ueConfig"  :value="form.content"
                 @change="form.content = $event"
        ></ueditor>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="$router.push({path:'index'})">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import api from '@/api/plugins'
import ueditor from '@/components/Ueditor/Ueditor.vue'
import { getToken } from '@/utils/auth'
export default {
  components: {
    ueditor
  },
  data() {
    return {
      form: {
        plugin_id: 0,
        min_version: 0,
        name: '',
        remark: '',
        icon: '',
        content: '',
        status: 1,
        path: '',
        plugin_tag: ''
      },
      ueConfig: {
        // 编辑器层级的基数,默认是900
        zIndex: 1000,
        // 编辑器自动被内容撑高
        autoHeightEnabled: false,
        // 初始容器高度
        initialFrameHeight: 300,
        // 初始容器宽度
        initialFrameWidth: '100%',
        serverUrl: process.env.VUE_APP_BASE_API + '/upload/ueditor/Authorization/' + getToken(),
        // 图片操作的浮层开关
        imagePopup: true,
        // 打开右键菜单功能
        enableContextMenu: true,
        // 是否保持toolbar的位置不动,默认true
        autoFloatEnabled: false,
        // 是否启用元素路径
        elementPathEnabled: false,

        // 当鼠标放在工具栏上时显示的tooltip提示
        labelMap: {
          //simpleupload: '插入图片'
        },
        //允许的最大字符数
        maximumWords: 10000,
        //字数统计提示，{#count}代表当前字数，{#leave}代表还可以输入多少字符数,留空支持多语言自动切换，否则按此配置显示
        wordCountMsg: '当前已输入{#count}个字符'
      }
    }
  },

  mounted() {
    if (this.$route.query.hasOwnProperty('id')) {
      this.getData(this.$route.query.id)
    }
  },

  methods: {
    getData(id) {
      api.info({ id: id }).then((res) => {
        this.form = res.data
      })
    },

    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let req
          if (this.form.id > 0) {
            req = api.update(this.form)
          } else {
            req = api.create(this.form)
          }
          req.then((res) => {
            this.$message({
              type: 'success',
              message: res.msg,
              duration: 500,
              onClose: () => {
                 this.$router.push({ path: 'index' })
              }
            })

          }).catch((error) => {

          })
        }
      });

    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}

.editor-content {
  margin-top: 20px;
}

.article-textarea ::v-deep {

  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }

}
</style>

